package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.logipoint.Logipoint;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.logipoint.ILogipointService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物流运作点信息Controller
 * Created by weih on 2016-10-09.
 */
@Controller
@RequestMapping("/mdm/logipoint")
public class LogipointController extends BaseController {

    @Autowired
    ILogipointService logipointService;
    /**
     * 跳转新增物流运作点页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/logipoint/add";
    }
    /**
     * 新增物流运作点信息
     * @param logipoint 物流运作点对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addLogipoint(@RequestBody Logipoint logipoint,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logipoint.setCreateUser(getSessionUser().getUserId());
        logipoint.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logipointService.addLogipoint(logipoint);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑物流运作点页面
     * @param logipointCode  物流运作点代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String logipointCode,Model model){
        model.addAttribute("logipoint", logipointService.getLogipointByCode(logipointCode));
        return "mdm/logipoint/edit";
    }

    /**
     * 编辑物流运作点信息
     * @param logipoint
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editLogipoint(@RequestBody Logipoint logipoint,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Logipoint oldLogipoint=logipointService.getLogipointByCode(logipoint.getLogipointCode());
        oldLogipoint.setPointId(logipoint.getPointId());
        oldLogipoint.setPointCode(logipoint.getPointCode());
        oldLogipoint.setPointName(logipoint.getPointName());
        oldLogipoint.setOrgType(logipoint.getOrgType());
        oldLogipoint.setCompanyCode(logipoint.getCompanyCode());
        oldLogipoint.setDealerCode(logipoint.getDealerCode());
        oldLogipoint.setNationCode(logipoint.getNationCode());
        oldLogipoint.setProvinceCode(logipoint.getProvinceCode());
        oldLogipoint.setCityCode(logipoint.getCityCode());
        oldLogipoint.setDistrictCode(logipoint.getDistrictCode());
        oldLogipoint.setStreet(logipoint.getStreet());
        oldLogipoint.setAddress(logipoint.getAddress());
        oldLogipoint.setPostcode(logipoint.getPostcode());
        oldLogipoint.setContact(logipoint.getContact());
        oldLogipoint.setMobiPhone(logipoint.getMobiPhone());
        oldLogipoint.setTel(logipoint.getTel());
        oldLogipoint.setFax(logipoint.getFax());
        oldLogipoint.setLatitude(logipoint.getLatitude());
        oldLogipoint.setLongitude(logipoint.getLongitude());
        oldLogipoint.setPointType(logipoint.getPointType());
        oldLogipoint.setWhPosition(logipoint.getWhPosition());
        oldLogipoint.setWhSynchronize(logipoint.getWhSynchronize());
        oldLogipoint.setWhNonconsign(logipoint.getWhNonconsign());
        oldLogipoint.setWhParentId(logipoint.getWhParentId());
        oldLogipoint.setWhProperty(logipoint.getWhProperty());
        oldLogipoint.setWhCapacity(logipoint.getWhCapacity());
        oldLogipoint.setWhInnerCapacity(logipoint.getWhInnerCapacity());
        oldLogipoint.setWhOuterCapacity(logipoint.getWhOuterCapacity());
        oldLogipoint.setVhCapacity(logipoint.getVhCapacity());
        oldLogipoint.setMaxVhCapacity(logipoint.getMaxVhCapacity());
        oldLogipoint.setWhPdi(logipoint.getWhPdi());
        oldLogipoint.setWhDriveway(logipoint.getWhDriveway());
        oldLogipoint.setWhCrossdock(logipoint.getWhCrossdock());
        oldLogipoint.setWhOffline(logipoint.getWhOffline());
        oldLogipoint.setWhWms(logipoint.getWhWms());
        oldLogipoint.setWhRealwh(logipoint.getWhRealwh());
        oldLogipoint.setWhStarttime(logipoint.getWhStarttime());
        oldLogipoint.setWhEndtime(logipoint.getWhEndtime());
        oldLogipoint.setWhLegacy(logipoint.getWhLegacy());
        oldLogipoint.setWhArea(logipoint.getWhArea());
        oldLogipoint.setWhEsc(logipoint.getWhEsc());
        oldLogipoint.setTotalStore(logipoint.getTotalStore());
        oldLogipoint.setLeadTime(logipoint.getLeadTime());
        oldLogipoint.setLoadTime(logipoint.getLoadTime());
        oldLogipoint.setStdInterface(logipoint.getStdInterface());
        oldLogipoint.setRecordPhone(logipoint.getRecordPhone());
        oldLogipoint.setNewDriveway(logipoint.getNewDriveway());
        oldLogipoint.setUpdateUser(getSessionUser().getUserId());
        oldLogipoint.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logipointService.updateLogipoint(oldLogipoint);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除物流运作点信息
     * @param logipoint 物流运作点对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteLogipoint(@RequestBody Logipoint logipoint,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logipoint.setUpdateUser(getSessionUser().getUserId());
        logipoint.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logipointService.deleteLogipoint(logipoint);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/logipoint/list";
    }

    /**
     * 分页查询数据
     * @param logipoint 物流运作点对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Logipoint> getLogipointData(Logipoint logipoint ,DataTableRequest dtrequest){
        int cnt = (int)logipointService.getLogipointCount(logipoint);
        List<Logipoint> list = logipointService.queryLogipoints(logipoint, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Logipoint> result = new PaginationResult<Logipoint>(list, pi);
        return new DataTableResponse<Logipoint>(dtrequest.getsEcho(),result);
    }

}
